package org.example.day05;

/**
 * 第二章节2.2.10: 数据类型String的常量池特性
 * ★尽量不使用String作为锁对象
 */
public class MyThread8 {
    public static void print(String stringParam){
        try {
            synchronized (stringParam){
                while(true){
                    System.out.println(Thread.currentThread().getName());
                    Thread.sleep(1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MyThread8 m8 = new MyThread8();
        Thread8A ta = new Thread8A(m8);
        ta.setName("A");
        ta.start();
        Thread8B tb = new Thread8B(m8);
        ta.setName("B");
        ta.start();
    }
}

class Thread8A extends Thread{
    private MyThread8 m8;

    public Thread8A(MyThread8 m8) {
        this.m8 = m8;
    }

    @Override
    public void run() {
        m8.print("AA");
    }
}
class Thread8B extends Thread{
    private MyThread8 m8;

    public Thread8B(MyThread8 m8) {
        this.m8 = m8;
    }

    @Override
    public void run() {
        m8.print("AA");
    }
}

//☆String常量池
//Stirng a = "AA";
//Stirng b = "AA";
//System.out.println(a == b)  true